﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;

namespace DllShepherd.SilverlightGoogleMaps.Common
{
    public partial class MapControl : UserControl
    {
        public MapControl()
        {
            InitializeComponent();
        }

        private void MapMouseMove(object sender, MouseEventArgs e)
        {
            if (MyMap.ScreenToMap(e.GetPosition(MyMap)) == null || MapMousePosition == null) return;
            MapMousePosition.X = MyMap.ScreenToMap(e.GetPosition(MyMap)).X;
            MapMousePosition.Y = MyMap.ScreenToMap(e.GetPosition(MyMap)).Y;
        }

        private void ArcGISTiledMapServiceLayer_TileLoaded(object sender, TiledLayer.TileLoadEventArgs e)
        {
            var backgroundLayer = (ArcGISTiledMapServiceLayer)MyMap.Layers[MapLayerNames.BackgroundLayer];
            backgroundLayer.Url = BackgroundUrl;
        }


        #region DEPENDENCY PROPERTY: BackgroundUrl

        /// <summary>Identifies the <see cref="BackgroundUrl"/> dependency property. This field is read-only.</summary>
        public static readonly DependencyProperty BackgroundUrlProperty = DependencyProperty.Register(
            "BackgroundUrl",
            typeof(string),
            typeof(MapControl),
            new PropertyMetadata(default(string), OnBackgroundUrlChanged)
            );

        /// <summary>Gets or sets the BackgroundUrl. This is a dependency property.</summary>
        public string BackgroundUrl
        {
            get { return (string)GetValue(BackgroundUrlProperty); }
            set { SetValue(BackgroundUrlProperty, value); }
        }

        /// <summary>
        /// BackgroundUrlProperty property changed handler.
        /// </summary>
        private static void OnBackgroundUrlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var mapControl = (MapControl)d;
            if (mapControl.MyMap == null)
            {
                return;
            }

            var backgroundLayer =
                (ArcGISTiledMapServiceLayer)mapControl.MyMap.Layers[MapLayerNames.BackgroundLayer];
            backgroundLayer.Url = (string)e.NewValue;
        }

        #endregion DEPENDENCY PROPERTY: BackgroundUrl

        #region DEPENDENCY PROPERTY: MapMousePosition

        //public MapPoint MapMousePosition { get; private set; }

        /// <summary>Identifies the <see cref="Profile"/> dependency property. This field is read-only.</summary>
        public static readonly DependencyProperty MapMousePositionProperty = DependencyProperty.Register(
            "MapMousePosition",
            typeof(MapPoint),
            typeof(MapControl),
            new PropertyMetadata(null)
            );

        /// <summary>Gets or sets the IsLayerControlVisibleProperty. This is a dependency property.</summary>
        public MapPoint MapMousePosition
        {
            get { return (MapPoint)GetValue(MapMousePositionProperty); }
            set { SetValue(MapMousePositionProperty, value); }
        }

        #endregion DEPENDENCY PROPERTY: MapMousePositionProperty
    }

    public class MapLayerNames
    {
        public static string BackgroundLayer = "BackgroundLayer";
    }
}
